package wang.aweb.childcar.controller.agentApi;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import wang.aweb.childcar.common.ApiRestResponse;
import wang.aweb.childcar.filter.AgentFilter;
import wang.aweb.childcar.model.pojo.Member;
import wang.aweb.childcar.model.pojo.WqPage;
import wang.aweb.childcar.model.query.OrderParam;
import wang.aweb.childcar.model.vo.MemberVO;
import wang.aweb.childcar.service.MemberService;
import wang.aweb.childcar.service.OrderService;
import wang.aweb.childcar.util.MonthDataUtils;

@RestController
@RequestMapping("/agentApi/orders/")
public class AgentOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private MemberService memberService;
    @PostMapping("getList")
    public ApiRestResponse getList(@RequestBody OrderParam orderParam){
        if(!StringUtils.isEmpty(orderParam.getDate())){
            orderParam.setStartTime(MonthDataUtils.getDayStart(orderParam.getDate()));
            orderParam.setEndTime(MonthDataUtils.getDayEnd(orderParam.getDate()));
        }
        if(!StringUtils.isEmpty(orderParam.getMobile())){
            MemberVO paraMember=memberService.selectByMoblile(orderParam.getMobile());
            if(paraMember!=null){
                orderParam.setMemberId(paraMember.getId());
            }else{
                orderParam.setMemberId(0);
            }
        }
        orderParam.setAgentId(AgentFilter.agentThreadLocal.get().getId());
        PageInfo pageInfo=orderService.getList(orderParam.getPage(),orderParam.getLimit(),orderParam);
        WqPage wqPage=new WqPage(pageInfo.getTotal(),pageInfo.getList());
        return ApiRestResponse.success(wqPage);
    }
}
